#!/usr/bin/perl -w

#####################################################################
# Program Variables - Go see the central config.pm!
#####################################################################
use FindBin qw($Bin);
use lib "$Bin";
use config;
use layout;

#####################################################################
# Program Modules
#####################################################################
# CGI Modules
use CGI;
use CGI::Carp;
use CGI::Pretty;

# Database Modules
use DBI;

# Data::Dumper for debugging
use Data::Dumper;

# POSIX Stuff...
use POSIX;

#####################################################################
# Program Start
#####################################################################
my $cgi = new CGI;

my $pagetitle = &read_config_option("tank_name") . " Index Page";

# Web Page output
&ts_pagestart($pagetitle);

#######################################
# Get the most recent statistics
my $dbh = DBI->connect("dbi:SQLite:dbname=$database_file", "", "");

my @check_times;
my @note_times;

if (!defined($cgi->param('full'))) {
    my $prevday = `date "+%Y-%m-%d %H:%M:%S" --date="-1 day"`;
    @check_times =
      @{ $dbh->selectcol_arrayref("SELECT DISTINCT collected FROM aquarium WHERE collected >= ? ORDER BY collected DESC", undef, $prevday) };
    @note_times = @{ $dbh->selectcol_arrayref("SELECT DISTINCT collected FROM notes WHERE collected >= ? ORDER BY collected DESC", undef, $prevday) };
    print $cgi->center($cgi->p("Showing the past 24 hours of records."));
} else {
    @check_times = @{ $dbh->selectcol_arrayref("SELECT DISTINCT collected FROM aquarium ORDER BY collected DESC") };
    @note_times  = @{ $dbh->selectcol_arrayref("SELECT DISTINCT collected FROM notes ORDER BY collected DESC") };
    print $cgi->center($cgi->p("Showing all records."));
}

my @eventtimes = (@check_times, @note_times);

my %seen = ();
my @uniquetimes = grep { !$seen{$_}++ } @eventtimes;

my @tablerows;

my @tableheaders = ("Collected");
my $test_sth     = $dbh->prepare("SELECT * FROM tests ORDER BY displayorder ASC");
$test_sth->execute();
while (my $testref = $test_sth->fetchrow_hashref()) {
    my %testhash = %{$testref};
    push(@tableheaders, $testhash{name});
}
$test_sth->finish();
undef $test_sth;

foreach $time (reverse sort @uniquetimes) {
    my %check_hash = %{ $dbh->selectall_hashref("SELECT * FROM aquarium WHERE collected=?", "name",      undef, $time) };
    my %note_hash  = %{ $dbh->selectall_hashref("SELECT * FROM notes WHERE collected=?",    "collected", undef, $time) };
    my @tabledata  = ("$time");

    my $test_sth = $dbh->prepare("SELECT * FROM tests ORDER BY displayorder ASC");
    $test_sth->execute();
    while (my $testref = $test_sth->fetchrow_hashref()) {
        my %testhash = %{$testref};
        my $name     = $testhash{name};
        push(@tabledata, $check_hash{$name}{value} // "&nbsp;");
    }

    my $entry = $note_hash{$time}{note};
    $entry //= "&nbsp;";
    $entry =~ s/\n/<br>/mg;

    push(@tablerows, $cgi->td([@tabledata]) . $cgi->td({ -class => "left" }, $entry));
}

# Close the database
$dbh->disconnect;
#######################################

# Output the display table
print $cgi->start_table({ -align => 'center', -width => "90%" });
print $cgi->Tr([ $cgi->th([ @tableheaders, "Log Entry/Note" ]) ]);
print $cgi->Tr([@tablerows]);
print $cgi->end_table;

# Show the result options
if (!defined($cgi->param('full'))) {
    print $cgi->center($cgi->p($cgi->a({ -href => "history.cgi?full=1" }, "Show all records")));
} else {
    print $cgi->center($cgi->p($cgi->a({ -href => "history.cgi" }, "Show records from the past 24 hours")));
}

# Close the page out.
print $cgi->p($cgi->center("Return to the " . $cgi->a({ -href => "./" }, &read_config_option("tank_name") . " Index") . " Page."));
print $cgi->end_html;
print "\n";
exit 0;

